package cn.vacuumflask.cordova.utils;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import com.orhanobut.logger.Logger;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;


/**
 * Created by Administrator on 2017/4/27 0027.
 * 日志输出工具类
 */

public class L {

    public static void i(String msg) {
        Log.i(Constant.TAG, "i---->" + msg);
    }

    public static void d(String msg) {
        Log.d(Constant.TAG, "d---->" + msg);
    }

    public static void e(String msg) {
        Log.e(Constant.TAG, "e---->" + msg);
    }

    public static void loggerI(String msg) {
        Logger.i(msg);
    }

    public static void loggerD(String msg) {
        Logger.d(msg);
    }

    public static void loggerE(String msg) {
        Logger.e(msg);
    }

    //打印json
    public static void json(String json) {
        Logger.json(json);
    }

    public static void array(Object array) {
        Logger.d(array);
    }

    public static void file(final Context context, final String msg) {
        Log.e(Constant.TAG, "file--->" + msg);
        MyThreadPool.getInstance().execute(new Runnable() {
            @Override
            public void run() {
                String path_logcat;
                if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {// 优先保存到SD卡中
                    path_logcat = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "shop_logcat";
                } else {// 如果SD卡不存在，就保存到本应用的目录下
                    path_logcat = context.getFilesDir().getAbsolutePath()
                            + File.separator + "shop_logcat";
                }
                File file = new File(path_logcat);
                if (!file.exists()) {
                    file.mkdirs();
                }
                File uniqueCodeFile = new File(file.getAbsolutePath() + "/" + "logcat.txt");
                FileOutputStream fos = null;
                ByteArrayInputStream inputStream = null;
                try {
                    if (!uniqueCodeFile.exists()) {
                        uniqueCodeFile.createNewFile();
                    }
                    fos = new FileOutputStream(uniqueCodeFile);
                    inputStream = new ByteArrayInputStream(msg.getBytes());
                    byte[] bytes = new byte[1024 * 4];
                    int len;
                    while ((len = inputStream.read(bytes)) != -1) {
                        fos.write(bytes, 0, len);
                    }
                    fos.flush();

                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (fos != null) {
                            fos.close();
                        }
                        if (inputStream != null) {
                            inputStream.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }
        });

    }


}
